package se2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import org.apache.xmlrpc.XmlRpcClient;
import org.apache.xmlrpc.XmlRpcException;

public class LoginBean
{

	private String	_username;
	private String	_password;

	public LoginBean()
	{
	}

	public void setUsername(String username_)
	{
		this._username = username_;
	}

	public String getUsername()
	{
		return this._username;
	}

	public void setPassword(String password_)
	{
		this._password = password_;
	}

	public String getPassword()
	{
		return this._password;
	}

	public void getMessage()
	{
		try
		{

			String server_url = "http://129.97.49.170:8080/RPC2";

			// Create an object to represent our server.
			XmlRpcClient server = new XmlRpcClient(server_url);

			// Build our parameter list.
			Vector params = new Vector();
			params.addElement(new String("hello, Brian grapes"));
			// params.addElement(new Integer(3));

			// Call the server, and get our result.
			// Hashtable result =
			server.execute("sample.adminTest", params);
			// int sum = ((Integer) result.get("sum")).intValue();
			// int difference = ((Integer) result.get("difference")).intValue();

			// Print out our result.
			// System.out.println("Sum: " + Integer.toString(sum) +
			// ", Difference: " +
			// Integer.toString(difference));

		}
		catch (XmlRpcException exception)
		{
			System.err.println("JavaClient: XML-RPC Fault #" + Integer.toString(exception.code) + ": "
					+ exception.toString());
		}
		catch (Exception exception)
		{
			System.err.println("JavaClient: " + exception.toString());
		}
	}

	public int getValidate()
	{
		try
		{
			Class.forName("org.sqlite.JDBC");
			String home = System.getProperty("user.home");
			Connection conn = DriverManager.getConnection("jdbc:sqlite:" + home + "/db/VoIP");
			Statement stat = conn.createStatement();

			String q = "select password from AdminInfo where username = '" + _username + "'";

			ResultSet rs = stat.executeQuery(q);
			if (rs.next() && rs.getString("password").equals(_password))
			{
				System.out.println("Valid login");
				rs.close();
				conn.close();
				return 1;
			}
			else
			{
				System.out.println("Invalid login");
				rs.close();
				conn.close();
				return 2;
			}
		}
		catch (Exception e)
		{
			System.out.println ("Validation failed.");
		}
		return 0;
	}

	public static void main(String[] args)
	{
		System.out.println("LoginBean.");
	}
}